package org.example.memora.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.memora.entity.Post;

import java.util.List;

@Mapper
public interface PostMapper  extends BaseMapper<Post> {
    @Update("UPDATE posts SET view_count = view_count + #{count} WHERE post_id = #{postId}")
    int incrementViewCount(@Param("postId") String postId, @Param("count") Integer count);
    // 获取兴趣相关帖子 ID
    @Select("${subQuery}")
    List<String> selectPostIdsBySubQuery(@Param("subQuery") String subQuery);

    // 获取最新的帖子 ID
    @Select("SELECT post_id FROM posts WHERE is_deleted = 0 AND status = 1 ORDER BY created_at DESC LIMIT #{limit}")
    List<String> selectLatestPostIds(@Param("limit") int limit);
    @Update("UPDATE posts SET comment_count = posts.comment_count + #{count} WHERE post_id = #{postId}")
    int incrementCommentCount(String postId, int count);
    @Update("UPDATE posts SET comment_count =  #{count} WHERE post_id = #{postId}")
    int clearCommentCount(String postId, int count);

    @Update("UPDATE posts SET like_count = posts.like_count + #{count} WHERE post_id = #{postId}")
    int incrementPostLikeCount(String postId, int count);
    @Select("SELECT like_count FROM posts WHERE post_id = #{postId}")
    int selectLikeCountInt(String postId);
    @Select("SELECT comment_count FROM posts WHERE post_id = #{postId} AND is_deleted = 0")
    int selectCommentCount(String postId);
    @Select("SELECT collection_count FROM posts WHERE post_id = #{postId} AND is_deleted = 0")
    int selectFavoriteCount(String postId);
    @Select({
            "<script>",
            "SELECT post_id FROM posts",
            "WHERE category_id = #{categoryId}",
            "AND post_id IN",
            "<foreach collection='postIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "AND is_deleted = 0",
            "AND status = 1",
            "</script>"
    })
    List<String> selectPostIdsByCategory(@Param("categoryId") Integer categoryId, @Param("postIds") List<String> postIds);
    @Select({
            "SELECT post_id FROM posts",
            "WHERE category_id = #{categoryId}",
            "AND is_deleted = 0",
            "AND status = 1",
            "ORDER BY created_at DESC",
            "LIMIT #{limit}"
    })
    List<String> selectLatestPostIdsByCategory(@Param("categoryId") Integer categoryId, @Param("limit") Integer limit);

}
